<?php

include_once APPLICATION_DIR . "/application/model/APPLICATION.php";

class Application extends DataObjects_APPLICATION
{
	const TYPE_APPLICATION	= 1;
	const TYPE_DESIGN		= 2;
	const TYPE_TECHNOLOGY	= 3;

	public function __construct($type = 0) {
		if ($type) {
			$this->TYPE = $type;
		}
	}

	public function validateInput(){ 
        global $error_msg;
        $posted_file = $_FILES["IMAGE"];
		if ($error_msg->isNoError() && !empty($posted_file["name"])) {
			if ($this->ID) {
				$this->OLD_IMAGE = $this->IMAGE;
			}
            $upload = new UPLOAD();
			$new_file = $upload->doUploadImage(IMAGE_DIR."/", $posted_file, '', '190', '');
			if ($new_file) {
				$this->IMAGE = $new_file;
			} else {
				$error_msg->add('_MSG_UPLOAD_FAILED');
			}
		}
        return $error_msg->isNoError();
    }

	public function update() {
		if (!empty($this->OLD_IMAGE) && $this->IMAGE != $this->OLD_IMAGE) {
			@unlink(IMAGE_DIR."/{$this->OLD_IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->OLD_IMAGE}");
		}

		return parent::update();
	}

	public function delete() {
		if ($this->IMAGE) {
			@unlink(IMAGE_DIR."/{$this->IMAGE}");
			@unlink(IMAGE_DIR."/thumb{$this->IMAGE}");
		}

		return parent::delete();
	}

	public static function deletable($id, $type) {
		// check if exists a child node?
		$app = new Application();
		$app->ID_PARENT = $id;
		$app->TYPE = $type;
		if($app->count()>0){
			return false;
		}

		return true;
	}

	public static function getTree($id_parent = 0, $type = 0) {
		$tree = array();

		// get all childs of current id_parent
        $app = new Application();
		$app->ID_PARENT = $id_parent;
		$app->TYPE = $type;
		$app->orderBy('POS, TITLE');
		$app->find();

        while($app->fetch()){
            $tree[$app->ID]['TITLE'] = $app->TITLE;
			$tree[$app->ID]['TYPE'] = $app->TYPE;
            $tree[$app->ID]['DESCRIPTION'] = $app->DESCRIPTION;
            $tree[$app->ID]['childs'] = self::getTree($app->ID, $type);
            $tree[$app->ID]['deletable'] = empty($tree[$app->ID]['childs']) ? true : false;
        }

		return $tree;
    }
    public static function getSubItem($id_parent, $type = 0){
        $app = new Application();
		$sql = "SELECT * FROM APPLICATION WHERE ID_PARENT = {$id_parent} AND TYPE = {$type} AND DISABLED = 0 ORDER BY POS ASC";
        $app->query($sql);
        return $app;
    }
    
    public static function getAllSubItem($id_parent,&$arrCat,$path, $type = 0){
        $app = new Application();
        $sql = "SELECT * FROM APPLICATION WHERE ID_PARENT = {$id_parent} AND TYPE = {$type} AND DISABLED = 0 ORDER BY POS ASC";
        $app->query($sql);
        while($app->fetch()){
            if(!isset($arrCat[$app->ID_PARENT])){
                $arrCat[$app->ID] = array();
            }
            $arrCat[$app->ID] = array("ID"=> $app->ID, "TITLE" => $app->TITLE);
            if(in_array($app->ID, $path)){
                $arrCat[$app->ID]["HAVE_SUB"] = true;
                self::getAllSubItem($app->ID,$arrCat[$app->ID]["SUB"],$path, $type);
            }
            else{
                $arrCat[$app->ID]["HAVE_SUB"] = false;
            }
        }
    }
    
    public static function getApplicationPath($id,&$path, $type = 0){
        $app = new Application();
        $app->ID = $id;
		$app->TYPE = $type;
        $app->find(1);
        if($app->ID_PARENT >0){
            $path[] = $app->ID_PARENT;
            self::getApplicationPath($app->ID_PARENT, $path, $type);
        }       
    }
    
    public function getRootItem($type){
        $this->TYPE = $type;
        $this->DISABLED = 0;
        $this->ID_PARENT = 0;
        $this->orderBy("POS ASC");
        $this->find();
    }
    
    public function getRelatedCategory($id_app){
        $sql = "SELECT * FROM `RELATED_CATEGORY` 
        INNER JOIN CATEGORY ON CATEGORY.ID = RELATED_CATEGORY.ID_CATEGORY 
        WHERE `ID_APPLICATION` = {$id_app}";
        $this->query($sql);
        
    }
    public static function getApplicationPathName($id,&$path, $type = 0){
        $app = new Application();
        $app->ID = $id;
		$app->TYPE = $type;
        $app->find(1);
        $path[] = array("ID" => $app->ID, "TITLE" => $app->TITLE);
        if($app->ID_PARENT >0){
            self::getApplicationPathName($app->ID_PARENT, $path, $type);
        }       
    }
}
?>
